﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using University.Workshop.Business;
using University.Workshop.Model;
using University.Workshop.Presenters.Views;
using University.Workshop.Presenters;
using System.IO;

namespace University.Workshop.WinApp
{
    public partial class NewProduct : Form,IProductView
    {
        OpenFileDialog openFile = new OpenFileDialog();
        ProductPresenter presenter;
        string ciUser;
        bool valid = false;
        public NewProduct()
        {
            InitializeComponent();
            presenter = new ProductPresenter(this);
            textBoxDateBuyProduct.Text = DateTime.Now.ToShortDateString();
            ciUser = Main.userMenu[3];
            //IssuePermits();
        }

        //private void IssuePermits()
        //{
        //    //buttonSave.Enabled = RoleFacade.PermissionUser(Main.userMenu[3], "");
        //}

        private void comboBoxGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable nameProduct = presenter.LoadProduct(comboBoxGroup.Text);
            comboBoxNameProduct.Items.Clear();
            for (int i = 0; i < nameProduct.Rows.Count; i++) 
            {
                comboBoxNameProduct.Items.Add(nameProduct.Rows[i]["Nameproduct"].ToString());
            }
            comboBoxNameProduct.Items.Add("Others");
            textBoxCodeProduct.Text = "";
            textBoxStockCurrent.Text = "";
        }

        private void comboBoxNameProduct_SelectedIndexChanged(object sender, EventArgs e)
        {            
            if (comboBoxNameProduct.Text == "Others")
            {
                Random random = new Random(DateTime.Now.Millisecond);
                textBoxNameProduct.ReadOnly = false;
                textBoxCodeProduct.Text = "1"+random.Next(10000);
                textBoxStockCurrent.Text = "0";
            }
            else
            {
                presenter.LoadNameProduct(comboBoxNameProduct.Text);
                textBoxNameProduct.Text = "";
                textBoxNameProduct.ReadOnly = true;
                errorProviderNewProduct.SetError(textBoxNameProduct, null);
            }
        }

        private void comboBoxSupplier_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxSupplier.Text == "Others")
            {
                textBoxSupplier.ReadOnly = false;
            }
            else
            {
                textBoxSupplier.Text = "";
                textBoxSupplier.ReadOnly = true;
                errorProviderNewProduct.SetError(textBoxSupplier, null);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if(ValidateFields.ValidateNumberFields()  && !string.IsNullOrEmpty(comboBoxGroup.Text) && valid==true)
            {
                if (presenter.SaveRegistrationProduct())
                {
                    try
                    {
                        File.Copy(openFile.FileName, Path.Combine(Application.StartupPath, string.Format("Pictures\\{0}", openFile.SafeFileName)));

                    }
                    catch (Exception)
                    {

                    }
                    this.Close();
                }
                else 
                {
                    MessageBox.Show("Error: Incorrect Data");
                }
            }
            else
            {
                valid = false;
                MessageBox.Show("All fields are required");
            }

        }
        private void buttonTotal_Click(object sender, EventArgs e)
        {
            textBoxTotalPrice.Text = ProductManager.TotalPriceBuy(textBoxUnitPrice.Text,textBoxStockOrder.Text);
            valid = true;
        }

        private void textBoxNameProduct_TextChanged(object sender, EventArgs e)
        {
            errorProviderNewProduct.SetError(textBoxNameProduct, ValidateFields.NameValidation(textBoxNameProduct.Text));
        }
        private void textBoxSupplier_TextChanged(object sender, EventArgs e)
        {
            errorProviderNewProduct.SetError(textBoxSupplier, ValidateFields.NameValidation(textBoxSupplier.Text));
        }
        private void textBoxStockOrder_TextChanged(object sender, EventArgs e)
        {
            errorProviderNewProduct.SetError(textBoxStockOrder,ValidateFields.NumbersInt(textBoxStockOrder.Text));
        }
        private void textBoxUnitPrice_TextChanged(object sender, EventArgs e)
        {
            errorProviderNewProduct.SetError(textBoxUnitPrice, ValidateFields.NumbersFloat(textBoxUnitPrice.Text));
        }
        public int NBuyProduct
        {            
            get
            {
                return int.Parse(textBoxBuyProduct.Text);
            }
            set
            {
                textBoxBuyProduct.Text = Convert.ToString(value + 1); 
            }
        }

        public string CIUser
        {
            get { return ciUser; }
        }

        public string DateBuy
        {
            get { return textBoxDateBuyProduct.Text; }
        }

        public string GroupProduct
        {
            get { return comboBoxGroup.Text; }
        }

        public string NameProduct
        {
            get
            {
                if (comboBoxNameProduct.Text == "Others")
                {
                    return textBoxNameProduct.Text;
                }
                else
                {
                    return comboBoxNameProduct.Text;
                }
            }
        }

        public string Supplier
        {
            get
            {
                if (comboBoxSupplier.Text == "Others")
                {
                    return textBoxSupplier.Text;
                }
                else
                {
                    return comboBoxSupplier.Text;
                }
            }
        }
        public int StockOrder
        {
            get { return int.Parse(textBoxStockOrder.Text); }
        }

        public float UnitPrice
        {
            get { return float.Parse(textBoxUnitPrice.Text); }
        }

        public float TotalPrice
        {
            get { return float.Parse(textBoxTotalPrice.Text); }
        }

        public string Description
        {
            get { return textBoxDescription.Text; }
        }

        public string PictureProduct
        {
            get { return openFile.SafeFileName; }
        }

        public string Message
        {            
            get
            {
                return null;
            }
            set
            {
                MessageBox.Show(value);
            }
        }

        public int CodeProduct
        {
            get
            {
                 return int.Parse(textBoxCodeProduct.Text); 
            }
            set
            {
                textBoxCodeProduct.Text = Convert.ToString(value);
            }
        }

        public int StockCurrent
        {
            get
            {
                return int.Parse(textBoxStockCurrent.Text);
            }
            set
            {
                textBoxStockCurrent.Text = Convert.ToString(value);
            }
        }

        private void buttonLoadPicture_Click(object sender, EventArgs e)
        {
            if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                pictureBoxProduct.ImageLocation = openFile.FileName;

            }
            else
            {
                pictureBoxProduct.Image = null;
            }
        }

        private void NewProduct_FormClosed(object sender, FormClosedEventArgs e)
        {
            Singleton.Instance.valid = true;
        }
    }
}
